package com.github.postsanf.yinian.gallery;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNCompassPic;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.List;

public class ImageAdapter extends BaseAdapter
{
    private Context context;
    private List<YNCompassPic> filmList;
    private ImageLoader imageLoader;

    public ImageAdapter(Context context, List<YNCompassPic> filmList,ImageLoader imageLoader) {
        this.context = context;
        this.filmList = filmList;
        this.imageLoader = imageLoader;
    }

    @Override
    public int getCount()
    {
        return filmList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return filmList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.timeline_third_image, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_third_image = (ImageView) convertView.findViewById(R.id.iv_third_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String path = YNUrlHelper.getTimeLineUrl(context, filmList.get(position).getPoriginal());
        imageLoader.displayImage(path, viewHolder.iv_third_image,
                ImageOptHelper.getTimeLineOptions(), new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        float scale = (float) loadedImage.getHeight() / loadedImage.getWidth();
                        int screenWidthPixels = DisplayUtils.getScreenWidthPixels((Activity) context) / 2;
                        int screenHeightPixels = DisplayUtils.getScreenHeightPixels((Activity) context)/2;
                        int height = (int) (screenWidthPixels * scale);
                        if (height > screenHeightPixels) {
                            height = screenHeightPixels;
                        }
                        GalleryFlow.LayoutParams params = new GalleryFlow.LayoutParams(screenWidthPixels, height);
                        viewHolder.iv_third_image.setLayoutParams(params);
                        viewHolder.iv_third_image.setImageBitmap(loadedImage);
                        // 抗锯齿
                        BitmapDrawable drawable = (BitmapDrawable) viewHolder.iv_third_image.getDrawable();
                        drawable.setAntiAlias(true);
                    }
                });
        viewHolder.iv_third_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return  viewHolder.iv_third_image;
    }

    class ViewHolder{
        ImageView  iv_third_image;
    }
}

